package com.nine.pattern.singleton;

import com.nine.pattern.singleton.demo4.Singleton;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author nine
 * @version 1.0
 * @description 序列化方式破坏单例
 * @date 2024-06-26 22:43
 */
public class TestClient01 {

	private static String filename = "E:/logs/a.txt";

	public static void main(String[] args) throws Exception {
		// writeObjectToFile();
		readObjectFromFile();
		readObjectFromFile();
	}

	private static void readObjectFromFile() throws Exception {
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename));
		Singleton singleton = (Singleton) ois.readObject();
		System.out.println(singleton);
		ois.close();
	}

	private static void writeObjectToFile() throws Exception {
		Singleton singleton = Singleton.getInstance();
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
		oos.writeObject(singleton);
		oos.close();
	}

}
